Inventory System

Created: 12/8/2022

Tags:

Unity | C# | Personal Work

I created this to allow for a dynamic inventory system that could allow new items with descriptions and abilities be added dynamically using Unity’s scriptable objects. When a new item was added to the inventory, the game would create a new prefab that would take the item’s information and display it in the inventory UI panel.

img Items were scriptable objects that had stats that could be filled out img Once the item was added to the inventory array, it would instantiate this prefab and insert the item’s data into the correct areas.
img Once items were added to the inventory, they would be displayed in a UI container that the player could scroll through img Once the potion was added to the inventory, any other calls to instantiate the potion would change to adding that amount to the prefab instead.

Inventory System Code:

          
            public class Inventory : MonoBehaviour
            {

                public GameObject itemPrefab;

                public List items = new List();

                #region Singleton

                public static Inventory instance;

                private void Awake()
                {
                    if (instance != null)
                    {

                        Debug.LogWarning("More than one instance of Inventory Found!");
                        return;
                    }
                    instance = this;



                }

                #endregion


                public void Add (ItemCreator item)
                {
                    for (int i = 0; i < items.Count; i++) // check to see if item is already in inventory list
                    {
                        if (items[i].name.Contains(item.name))
                        {
                            Debug.Log("Adding 1 to " + item.name);
                            item.amount++;
                            return;
                        }
                    }
                    Debug.Log("Item " + item + " added to inventory");
                    items.Add(item);

                    item.amount = 1; // Resets amount to 1 on spawn                                                 << (Temporary or Permanent)
                    GameObject temp = Instantiate(itemPrefab, transform.parent); // creates new item ui

                    temp.GetComponent().item = item; // sets new item to display selected itemDisplay
                    temp.transform.SetParent(gameObject.transform);

                }


                public void Remove (ItemCreator item)
                {
                    for (int i = 0; i < items.Count; i++) // check to see if item is already in inventory list      << (Temporary or Permanent)
                    {
                        if (items[i].name.Contains(item.name) && item.amount > 1) // only minus if there is more than 1 in the inventory
                        {
                            Debug.Log("Removing 1 to " + item.name);
                            item.amount--;
                            return;
                        }
                    }
                    item.amount--;
                    Debug.Log("Removing " + item.name + " from the list");
                    items.Remove(item);
                }
            }